/**
 * 广告弹层模块
 */
define(function (require, exports, module) {
    require('jquery');
    var popup = require('/static_public/node_modules/artdialog/src/popup');
    var util = require("util");

    var now = function () {
        if (typeof window.GET_SERVER_TIME === 'function') {
            return window.GET_SERVER_TIME();
        } else {
            return new Date();
        }
    };

    /**
     * 资源加载
     * @param {Array.<string>} urls
     * @param {function(Array.<{url:string,loaded:boolean,complete:boolean}>)} callback 加载完成回调
     * @constructor
     */
    var ResourceLoader = function (urls, callback) {
        var configs = [];
        for (var i = 0; url = urls[i]; i++) {
            configs.push({url: url, loaded: false, complete: false});
        }

        function onFinish(i, loaded) {
            return function () {
                var configItem = configs[i];
                configItem.loaded = loaded;
                configItem.complete = true;

                if (typeof callback === 'function') {
                    var flag = true;

                    var config;
                    for (var j = 0; config = configs[j]; j++) {
                        if (!config.complete) {
                            flag = false;
                            break;
                        }
                    }

                    if (flag) {
                        callback(configs);
                    }
                }
            }
        }

        var url;
        for (var i = 0; url = urls[i]; i++) {
            var img = new Image();

            img.onload = onFinish(i, true);
            img.onerror = onFinish(i, false);

            img.src = url;
        }
    };

    return {
        /**
         * 展示弹层
         * @param {string} html
         * @param {{close?:Function,show?:Function,timeout?:number}?} opt
         */
        showPopup: function (html, opt) {
            opt = opt || {};

            var pop = new popup();
            pop.innerHTML = html;
            pop.align = 'middle middle';
            pop.fixed = true;

            var $node = $(pop.node);

            var onShow = opt.show;
            pop.addEventListener('show', function () {
                if (typeof onShow === 'function') {
                    onShow(pop);
                }
            });

            pop.showModal();

            var $jsPopupContent = $node.find('.js_popup_content');
            if ($jsPopupContent[0]) {
                $jsPopupContent.css('maxHeight', $(window).height() - 120);
            }

            $node.on('click', '.js_close_btn', function () {
                pop.remove();
            });

            var onClose = opt.close;

            var autoCloseTimer;
            pop.addEventListener('remove', function () {
                clearTimeout(autoCloseTimer);

                if (typeof onClose === 'function') {
                    onClose(pop);
                }
            });

            var timeout = opt.timeout;
            if (typeof timeout !== 'undefined') {
                autoCloseTimer = setTimeout(function () {
                    pop.remove();
                }, timeout);
            }

            return pop;
        },
        /**
         * @param {{
         * key:string,
         * html:string,
         * showPop?:boolean,
         * showCallback?:Function,
         * expires?:number,
         * startTime?:number,
         * endTime?:number,
         * timeout?:number,
         * close?:Function,
         * show?:Function,
         * preLoadImages?:Array.<string>
         * }} opt
         */
        run: function (opt) {
            var key = opt.key;
            var html = opt.html;
            var expires = opt.expires;
            var startTime = opt.startTime;
            var endTime = opt.endTime;
            var showPop = opt.showPop;
            var showCallback = opt.showCallback;
            var preLoadImages = opt.preLoadImages;

            if (typeof showPop === 'undefined') {
                showPop = true;
            }

            var show = false;
            var setShow = true;
            if (typeof expires !== 'undefined') {
                show = setShow = util.getCookie(key) != '1';
            }

            if (typeof startTime !== 'undefined') {
                show = setShow = setShow && now().getTime() >= startTime;
            }

            if (typeof endTime !== 'undefined') {
                show = setShow = setShow && now().getTime() < endTime;
            }

            var self = this;

            if (show) {
                if (typeof showCallback === 'function') {
                    showCallback();
                }

                if (showPop) {
                    var run = function () {
                        self.showPopup(html,
                            {
                                close: function (pop) {
                                    var onClose = opt.close;
                                    if (typeof onClose === 'function') {
                                        onClose(pop);
                                    }

                                    if (typeof expires !== 'undefined') {
                                        util.setCookie(key, '1', expires, '/', document.domain);
                                    }
                                },
                                timeout: opt.timeout,
                                show: opt.show
                            }
                        );
                    };

                    if (preLoadImages) {
                        new ResourceLoader(preLoadImages, run);
                    } else {
                        run();
                    }
                }
            }
        }
    };
});